/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.abc.salesinventory.util;

import com.abc.salesinventory.service.newpackage.MasterService;
import com.abc.salesinventory.service.newpackage.MasterServiceImpl;
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.swing.JRViewer;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author rdjayawe
 */
public class ReportViewer extends javax.swing.JInternalFrame {

    private static String reportPath = null;
    MasterService masterService = new MasterServiceImpl();

    /**
     * Creates new form ReportViewer1
     */
    public ReportViewer(String fileName, HashMap params) {
        initComponents();

        if (reportPath == null) {
            String tempPath = (String) masterService.getPreference(MasterService.PREF_REPORT_LOCATION);

            if (tempPath == null || tempPath.equals("")) {
                JOptionPane.showMessageDialog(null, "Error in generating Reports !! Report path needs to be set through preferences", "Error", 0);
                return;
            } else {
                reportPath = tempPath;
            }
        }

        try {
            Connection con = Database.con();

            InputStream stream = new FileInputStream(new File(reportPath + fileName));
            JasperDesign jasperDesign = JRXmlLoader.load(stream);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, con);
            JRViewer viewer = new JRViewer(jasperPrint);
            
            this.setContentPane(viewer);

        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
            System.out.println("ERROR: " + cnfe.getMessage());
            JOptionPane.showMessageDialog(null, "Error in generating Reports !!" + cnfe.getMessage(), "Error", 0);
        } catch (SQLException sqle) {
            sqle.printStackTrace();
            System.out.println("ERROR: " + sqle.getMessage());
            JOptionPane.showMessageDialog(null, "Error in generating Reports !!" + sqle.getMessage(), "Error", 0);
        } catch (JRException jre) {
            jre.printStackTrace();
            System.out.println("ERROR: " + jre.getMessage());
            JOptionPane.showMessageDialog(null, "Error in generating Reports !!" + jre.getMessage(), "Error", 0);
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error in generating Reports !!" + e.getMessage(), "Error", 0);
            System.out.println("ERROR: " + e.getMessage());
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setClosable(true);
        setMaximizable(true);
        setResizable(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 961, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 609, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
